package com.hyx.java.ctrl.app;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.json.JSONObject;
import com.hyx.java.base.BaseResult;
import com.hyx.java.base.BaseCtrl;
import com.hyx.java.dto.HyxUserPwdReq;
import com.hyx.java.entity.HyxUser;
import com.hyx.java.mapper.HyxUserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/app")
@CrossOrigin(origins = "*")
@Api(value = "app端接口",tags = "app端接口")
public class HyxAppCtrl extends BaseCtrl {

    @Autowired
    private HyxUserMapper userMapper;
    @ApiOperation(value = "修改密码")
    @RequestMapping(value = "/update-password", method = RequestMethod.POST)
    public BaseResult<JSONObject> updatePassword(@RequestBody HyxUserPwdReq userPwd) {
        if (userPwd == null || StrUtil.isEmpty(userPwd.getOldPassword()) || StrUtil.isEmpty(userPwd.getNewPassword())){
            return BaseResult.fail("011", "指定参数为空");
        }
        HyxUser cacheUser = getUser();
        HyxUser user = userMapper.selectById(cacheUser.getId());
        if (!user.getPassword().equals(DigestUtil.md5Hex(userPwd.getOldPassword()))) {
            return BaseResult.fail("012", "原密码错误");
        }
        userMapper.updateById(HyxUser.builder().id(user.getId()).password(DigestUtil.md5Hex(userPwd.getNewPassword())).build());
        return BaseResult.success();
    }
}
